package com.example.foo.api.web.order;

import com.ruoyi.common.core.domain.AjaxResult;
import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.api.boot.TokenHelper;
import com.example.foo.order.model.Order;
import com.example.foo.order.query.OrderQuery;
import com.example.foo.order.service.OrderService;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Objects;

@org.springframework.web.bind.annotation.RestController
public class OrderAction {

    private final OrderService orderService;

    public OrderAction(OrderService orderService) {
        this.orderService = orderService;
    }

    @GetMapping("/api/orders")
    public AjaxResult index(OrderQuery orderQuery, Pageable pageable) {
        Long userId = TokenHelper.verifyToken();
        if (!Objects.equals(userId, orderQuery.getUid())) {
            return AjaxResult.error(403, "Access Forbidden");
        }
        orderQuery.setPayStatus(Order.PayStatus.processed.val);
        PageResult<Order> pageResult = orderService.findOrders(orderQuery, pageable);
        return AjaxResult.successData(pageResult);
    }

    @GetMapping("/api/orders/{id}")
    public AjaxResult showOrder(@PathVariable("id") Long id) {
        Long userId = TokenHelper.verifyToken();
        Order order = orderService.findOrderById(id);
        if (order == null) {
            return AjaxResult.error(404, "Not Found");
        }
        if (!Objects.equals(userId, order.getUid())) {
            return AjaxResult.error(403, "Access Forbidden");
        }
        return AjaxResult.successData(order);
    }

    /**
     * 确认收货
     */
    @PutMapping("/api/orders/{id}/received")
    public AjaxResult updateOrderReceived(@PathVariable("id") Long id) {
        Long userId = TokenHelper.verifyToken();
        Order order = orderService.findOrderById(id);
        if (order == null) {
            return AjaxResult.error(404, "Not Found");
        }
        if (!Objects.equals(order.getUid(), userId)) {
            return AjaxResult.error(403, "Access Forbidden");
        }
        if (!Objects.equals(order.getStatus(), Order.Status.delivery.val)) {
        	return AjaxResult.error(400, String.format("Order#%s 状态错误: %s", order.getId(), order.getStatus()));
        }

        order.setStatus(Order.Status.finished.val);
        orderService.updateOrder(order);

        return AjaxResult.success();
    }

    /**
     * 确认已自提
     */
    @PutMapping("/api/orders/{id}/selfWithdrawal")
    public AjaxResult selfWithdrawal(@PathVariable("id") Long id) {
        Long userId = TokenHelper.verifyToken();
        Order order = orderService.findOrderById(id);
        if (order == null) {
            return AjaxResult.error(404, "Not Found");
        }
        if (!Objects.equals(order.getUid(), userId)) {
            return AjaxResult.error(403, "Access Forbidden");
        }

        if (!Objects.equals(order.getPayStatus(), Order.PayStatus.processed.val)) {
            return AjaxResult.error(400, "该订单未支付");
        }

        order.setSelfWithdrawalTime(LocalDateTime.now());
        order.setStatus(Order.Status.finished.val);
        orderService.updateOrder(order);

        return AjaxResult.success();
    }

}
